<?php

namespace App\Http\Controllers\Admin;

use App\CodeResponse;
use App\Models\Goods;
use App\Models\GoodsSku;
use App\Services\GoodsService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;



class GoodsController extends AdminController
{
    //保存
    public function save(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'integer|digits_between:1,20|min:1',
            'goods_brand_id' => 'required|integer|digits_between:1,20|min:1',
            'goods_category_path_ids' => 'required|string',
            'title' => 'required|string',
            'goods_num' => 'required|string',
            'unit' => 'required|string',
            'keywords' => 'required|string',
            'is_show_stock' => 'required|integer',
            'production_time' => 'required|date',
            'image' => 'required|string',
            'carousel_image' => 'required|string',
            'description' => 'required|string',
            'status' => 'required|integer',
            'recommend' => 'required|integer',
            'goods_specs_ids' => 'required|string',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }

        $putData = $request->except(['_method', '_token','sku']);
        $goods_category_path_ids = $request->input('goods_category_path_ids');
        $categoryArr = explode(',',$goods_category_path_ids);
        if(count($categoryArr) > 0){
            $putData['goods_category_id'] = $categoryArr[count($categoryArr)-1];
            $putData['goods_category_path_ids'] = $goods_category_path_ids;
        }
        $id = $request->input('id');
        $flight = Goods::updateOrCreate(
            ['id' => $id],
            $putData
        );
        $goodsId = $flight->id;
        $skuArr = $request->input('sku');

        $skuArr = is_array($skuArr) ? $skuArr : json_decode($skuArr,true);

        $slide = GoodsSku::where('goods_id', $goodsId);
        $slide->delete();
        if(is_array($skuArr)){
            foreach ($skuArr as $v ){
                $v['goods_id'] = $goodsId;
                $v['stock'] = $v['stock'] ;
                GoodsSku::create($v);
            }
        }
        //更新库存
        $sum= GoodsSku::where('goods_id', $goodsId)->sum('stock');
        $goodsSku = GoodsSku::firstWhere('goods_id', $goodsId); //获取第一条记录
        $goodsModel = Goods::find($goodsId);
        $goodsModel->stock = $sum;
        if($goodsSku){
            $goodsModel->sku_id = $goodsSku->id;
        }
        $goodsModel->save();
        return $this->success($goodsId);
    }
    //列表
    public function  list(Request $request){
        $validator = Validator::make($request->all(), [
            'goods_brand_id' => 'integer',
            'goods_category_id' => 'integer',
            'status' => 'integer',
            'title' => 'string',
            'page' => 'integer',
            'limit' => 'integer',
            'sort' => 'string',
            'order' => 'string',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $goods_brand_id = $request->input('goods_brand_id');
        $goods_category_id = $request->input('goods_category_id');
        $status = $request->input('status');
        $title = $request->input('title');
        $page = $request->input('page',1);
        $limit = $request->input('limit',10);
        $sort = $request->input('sort','created_at');
        $order = $request->input('order','asc');

        $list = GoodsService::getInstance()->list($page,$limit,$sort,$order,$goods_brand_id,$goods_category_id,$title,$status);
        return $this->success($list);
    }

    //获取更新信息
    public function updateInfo(Request $request){
        $validator = Validator::make($request->all(), [
            'goodsId' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $info = GoodsService::getInstance()->updateInfo($request->goodsId);

        return $this->success($info);
    }
    //获取信息
    public function getSkuInfo(Request $request){
        $validator = Validator::make($request->all(), [
            'skuId' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $info = GoodsService::getInstance()->getSkuInfo($request->skuId);
        return $this->success($info);
    }

    //修改状态
    public function state(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
            'status' => 'required|integer',
        ]);
        if ($validator->fails() || !in_array($request->status,[0,1,2])) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $goods = Goods::find($request->id);
        $goods->status = $request->status;
        $goods->save();
        return $this->success();
    }

    // 图上传
    public function img(Request $request)
    {
        $url = config('filesystems.disks.goods.url');
        $default = config('filesystems.disks.goods.default');
        $file = GoodsService::getInstance()->upfile($request, $url, $default, 'goods','img');
        if(!$file){
            return $this->fail(CodeResponse::IMAGE_INCORRECT_FORMAT,'不支持当前图片扩展名');
        }
        return $this->success([
                'file' => $file,
            ]
        );
    }
}
